<?php
/**
 *
 * 取当前页面的相邻页面
 * @author LeoNing
 *
 */
class SiblingDataProvider implements IDataProvider {
	/* (non-PHPdoc)
	 * @see IDataProvider::provide()
	 */
	public function provide($params) {
		global $_PAGE;
		static $pM = false;
		extract ( $params );
		$page_id = intval ( $_PAGE ['page_id'] );
		if(!$pM){
			$pM = new WebPageModel ();
		}
		$fields = 'WP.*,WPC1.name as category_name,WPC1.url AS category_url,WPC2.name AS subcategory_name,WPC2.url AS subcategory_url,WPC1.bind,WPC1.home as domain_home';
		
		$where = " WP.deleted = 0 AND WP.status = 'published' AND page_type <> 'template' ";
		
		$sort = " ORDER BY WP.page_id ";
		
		if (isset ( $next )) {
			$where .= " AND WP.page_id > {$page_id}";
			$sort .= " ASC ";
		} else {
			$where .= " AND WP.page_id < {$page_id}";
			$sort .= " DESC ";
		}
		
		$from = ' ((web_page AS WP LEFT JOIN web_page_category AS WPC1 ON (WP.category = WPC1.name)) LEFT JOIN web_page_category AS WPC2 ON (WP.subcategory = WPC2.name))';
		
		$sql = "SELECT {$fields} FROM {$from} WHERE {$where} {$sort}";
		
		$pages = $pM->query ( $sql, array ('start' => 0, 'limit' => 1 ) );
		
		if ($pages) {
			return new CtsDataSet ( $pages );
		} else {
			return new CtsDataSet ( array () );
		}
	}
}